package com.tbit.uqbike.client.util;

import lombok.extern.slf4j.Slf4j;

import java.math.BigInteger;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串实体类
 *
 * @author Leon
 * 2014-4-11 上午11:42:24
 */
@Slf4j
public class StringUtil {

    /**
     * 分解字符串(整形)
     */
    public static List<Integer> getIntegerList(String param) {
        String[] params = param.split(",");
        List<Integer> resultList = new ArrayList<Integer>();
        for (String par : params) {
            resultList.add(Integer.parseInt(par));
        }

        return resultList;
    }

    /**
     * 构造字符串
     */
    public static String getListString(List<Integer> ids) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = ids.size(); i < length; i++) {
            result.append(ids.get(i));

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 构造字符串
     */
    public static String getArrayListString(List<String> paramList) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = paramList.size(); i < length; i++) {
            result.append("'" + paramList.get(i) + "'");

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 构造HTTP字符串
     */
    public static String getArrayListStringHttp(List<String> paramList) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = paramList.size(); i < length; i++) {
            result.append(paramList.get(i));

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 分解字符串（double）
     */
    public static List<Double> getDoubleList(String param) {
        String[] params = param.split(",");
        List<Double> resultList = new ArrayList<Double>();
        for (String par : params) {
            resultList.add(Double.parseDouble(par));
        }

        return resultList;
    }

    /**
     * 获取n位随机数
     *
     * @param length 随机数长度
     * @return
     */
    @SuppressWarnings("unchecked")
    public static String getAuthCode(Integer length) {
        String str = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        @SuppressWarnings("rawtypes")
        List list = new ArrayList();
        while (sb.length() < length) {
            int number = random.nextInt(10);
            if (sb.length() > 0 && !"0".equals(sb.substring(0, 1))) {
                int n = 0;
                for (int i = 0; i < list.size(); i++) {
                    if (!((Object) number).equals(list.get(i))) {
                        n++;
                    }
                }
                if (n == sb.length()) {
                    sb.append(str.charAt(number));
                    list.add(number);
                }
            } else {
                if (!"0".equals(String.valueOf(str.charAt(number)))
                        && sb.length() == 0) {
                    sb.append(str.charAt(number));
                    list.add(number);
                }
            }
        }

        return sb.toString();
    }

    /**
     * 判断字符串是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    /**
     * 判断是不是手机号码
     *
     * @param mobiles
     * @return
     */
    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 时间格式转化
     *
     * @param datetime 时间字符串 20220819153140
     * @return
     */
    public static String parseTime(String datetime) {
        //20220819153140
        StringBuffer stringBuffer = new StringBuffer(datetime);
        stringBuffer.insert(12, ":");
        stringBuffer.insert(10, ":");
        stringBuffer.insert(8, " ");
        stringBuffer.insert(6, "-");
        stringBuffer.insert(4, "-");
        return stringBuffer.toString();
    }

    /**
     * 获取uuid
     */
    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 获取骑行订单号
     *
     * @return
     */
    public static String getRideLogOrderNO() {
        // 10位随机数
        return DateTimeUtil.getNowTimeyyyyMMdd() + getRandomString(10);
    }



    /**
     * 获取指定位数随机数
     *
     * @param number
     * @return
     */
    public static String getRandomString(int number) {
        StringBuilder result = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < number; i++) {
            result.append(random.nextInt(10));
        }

        return result.toString();
    }

    public static String getBirth(String idNO) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(idNO.substring(6, 10));
        stringBuffer.append("-");
        stringBuffer.append(idNO.substring(10, 12));
        stringBuffer.append("-");
        stringBuffer.append(idNO.substring(12, 14));
        return stringBuffer.toString();
    }

    /**
     * 获取软件版本号支持功能
     *
     * @param version
     * @param index
     * @return
     */
    public static String getHardCode(String version, String index) {
        if (!version.contains(index)) {
            return null;
        }
        String tmp = version.substring(version.lastIndexOf(index) + 2, version.lastIndexOf(index) + 6);
        return tmp;
    }

    /**
     * 16进制转2进制
     *
     * @param hexString
     * @return
     */
    public static String hexString2binaryString(String hexString) {
        //16进制转10进制
        BigInteger sint = new BigInteger(hexString, 16);
        //10进制转2进制
        String result = sint.toString(2);
        //字符串反转
        return new StringBuilder(result).toString();
    }

    /**
     * 获取第几位数据
     *
     * @param hexString
     * @param num
     * @return
     */
    public static boolean getIndex(String hexString, Integer num) {
        BigInteger sint = new BigInteger(hexString, 16);
        return ((1 << num) & sint.intValue()) != 0;
    }


    //  000010011
//	000000010
//  000000100
//  000001000
//  000010000
    public static void main(String[] args) {
        /*Set<String> orderNOSet = new HashSet<>();
        for (int i = 0;i < 5000;i++){
            String rideLogOrderNO = getRideLogOrderNO();
            if(i < 10){
                System.out.println(rideLogOrderNO);
            }
            orderNOSet.add(rideLogOrderNO);
        }
        System.out.println("订单号数:"+orderNOSet.size());*/
        //NS_TBIT_WD-219_R.2.0.4_10-18-2022 11:29:57 &T650_G.C.OPENCPU_ZB_WM-007-007-005_ATGM_ACC_BLE_ECU_N58_OTA_H-0101_S-000F
        //NS_TBIT_WD-219_R.2.0.4_10-18-2022 11:29:57 &T650_G.C.OPENCPU_ZB_WM-007-007-005_ATGM_ACC_BLE_ECU_N58_OTA_H-0101_S-000F
        /*BigInteger sint = new BigInteger("0101", 16);
        System.out.println(sint);
        System.out.println(getIndex("000F", 0));
        System.out.println(getIndex("000F", 1));
        System.out.println(getIndex("000F", 2));
        System.out.println(getIndex("000F", 3));*/
    }

}
